/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditUser.java
 *
 * Created on 22-05-2011, 21:42:03
 */
package UpdateForm;

import ConnectData.Config;
import ConnectData.getConnections;
import Management.AdminForm;
import Register.MyMD5;
import UploadData.Database;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author s0lo
 */
public class EditUser extends javax.swing.JDialog {

    private String UserName;
    private String RePass;
    private String UName, LV, STS, PassWord;
    AdminForm parent;

    /** Creates new form EditUser */
    public EditUser(java.awt.Frame parent, boolean modal, String UName, String PWD, String lv, String status) {
        super(parent, modal);
        initComponents();
        this.UserName = UName;
        this.parent = (AdminForm) parent;
        this.UName = UName;
        this.PassWord = PWD;
        this.LV = lv;
        this.STS = status;
        txtUserName1.setText(UName);
       // pwfPass.setText(PWD);
        if (LV.equals("1")) {
            rdAdministrator.setSelected(true);
        } else {
            rdUser.setSelected(true);
        }

        if (status.equals("1")) {
            rdStatusON.setSelected(true);
        } else {
            rdStatusOFF.setSelected(true);
        }
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        btnRegister = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        rdAdministrator = new javax.swing.JRadioButton();
        rdUser = new javax.swing.JRadioButton();
        jLabel5 = new javax.swing.JLabel();
        rdStatusON = new javax.swing.JRadioButton();
        rdStatusOFF = new javax.swing.JRadioButton();
        txtUserName1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        pwfPass = new javax.swing.JPasswordField();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(204, 255, 204));

        jPanel3.setBackground(new java.awt.Color(204, 255, 204));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Information User", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12), new java.awt.Color(0, 0, 51))); // NOI18N

        btnRegister.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnRegister.setForeground(new java.awt.Color(0, 0, 51));
        btnRegister.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/save.png"))); // NOI18N
        btnRegister.setText("Save");
        btnRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegisterActionPerformed(evt);
            }
        });

        btnClose.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnClose.setForeground(new java.awt.Color(0, 0, 51));
        btnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/close1.png"))); // NOI18N
        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setForeground(new java.awt.Color(0, 0, 51));
        jLabel1.setText("Level :");

        rdAdministrator.setBackground(new java.awt.Color(204, 255, 204));
        buttonGroup1.add(rdAdministrator);
        rdAdministrator.setFont(new java.awt.Font("Tahoma", 1, 11));
        rdAdministrator.setForeground(new java.awt.Color(0, 0, 51));
        rdAdministrator.setText("Administrator");

        rdUser.setBackground(new java.awt.Color(204, 255, 204));
        buttonGroup1.add(rdUser);
        rdUser.setFont(new java.awt.Font("Tahoma", 1, 11));
        rdUser.setForeground(new java.awt.Color(0, 0, 51));
        rdUser.setText("User");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setForeground(new java.awt.Color(0, 0, 51));
        jLabel5.setText("Status : ");

        rdStatusON.setBackground(new java.awt.Color(204, 255, 204));
        buttonGroup2.add(rdStatusON);
        rdStatusON.setFont(new java.awt.Font("Tahoma", 1, 11));
        rdStatusON.setForeground(new java.awt.Color(0, 0, 51));
        rdStatusON.setText("Allow");

        rdStatusOFF.setBackground(new java.awt.Color(204, 255, 204));
        buttonGroup2.add(rdStatusOFF);
        rdStatusOFF.setFont(new java.awt.Font("Tahoma", 1, 11));
        rdStatusOFF.setForeground(new java.awt.Color(0, 0, 51));
        rdStatusOFF.setText("Deny");

        txtUserName1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtUserName1ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setForeground(new java.awt.Color(0, 0, 51));
        jLabel3.setText("Username :");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setForeground(new java.awt.Color(0, 0, 51));
        jLabel4.setText("Password :");

        pwfPass.setText("1234567");
        pwfPass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pwfPassActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3))
                        .addGap(20, 20, 20)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(pwfPass)
                            .addComponent(txtUserName1, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel1))
                        .addGap(9, 9, 9)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rdStatusON)
                            .addComponent(btnRegister)
                            .addComponent(rdAdministrator))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnClose)
                            .addComponent(rdStatusOFF)
                            .addComponent(rdUser))))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(24, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtUserName1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pwfPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(rdAdministrator)
                            .addComponent(rdUser))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(rdStatusON)
                            .addComponent(rdStatusOFF)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(82, 82, 82)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnRegister)
                            .addComponent(btnClose))))
                .addContainerGap())
        );

        jPanel2.setBackground(new java.awt.Color(153, 255, 153));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel2.setForeground(new java.awt.Color(255, 0, 0));
        jLabel2.setText("             EDIT USER");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnRegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegisterActionPerformed
        boolean valid = validateInformation();
        if (valid) {
            Connection cn = new Config().cn;
            PreparedStatement pst = null;
            try {
                String strUpd = "Update Users set UserName=?,UserPassword=?,UserLevel=?,Status=? where UserName=?";
                pst = cn.prepareCall(strUpd);
                pst.setString(1, txtUserName1.getText().toString());
                String pwNew = "";
                pwNew = MyMD5.getMD5String(pwfPass.getText());
                pst.setString(2, pwNew);
                int level, status;
                if (rdAdministrator.isSelected()) {
                    level = 1;
                } else {
                    level = 2;
                }
                if (rdStatusON.isSelected()) {
                    status = 1;
                } else {
                    status = 0;
                }
                pst.setInt(3, level);
                pst.setInt(4, status);
                pst.setString(5, UserName);
                int kq = pst.executeUpdate();
                if (kq != -1) {
                    JOptionPane.showMessageDialog(this, "Edit successful!");
                    parent.loadUSers();
                    this.dispose();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                try {
                    getConnections.closeObject(pst);
                    getConnections.closeObject(cn);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    }//GEN-LAST:event_btnRegisterActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_btnCloseActionPerformed

    private void txtUserName1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtUserName1ActionPerformed
        // TODO add your handling code here:
        btnRegisterActionPerformed(evt);
        pwfPass.requestFocus();
}//GEN-LAST:event_txtUserName1ActionPerformed

    private void pwfPassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pwfPassActionPerformed
        // TODO add your handling code here:
        btnRegisterActionPerformed(evt);
}//GEN-LAST:event_pwfPassActionPerformed
    /**
     * @param args the command line arguments
     */
    public boolean validateInformation() {

        String userN = txtUserName1.getText();
        if (userN.equals("")) {
            JOptionPane.showMessageDialog(this, "You must input User name!");
            txtUserName1.requestFocus();
            return false;
        } else {
            if (!userN.equals(UName)) {
                Vector vtUser = new Database().loadUsers(" where UserName='" + userN + "' ORDER BY UserName");
                if (vtUser.size() != 0) {
                    JOptionPane.showMessageDialog(this, "This user name is already exist!");
                    vtUser.removeAllElements();
                    txtUserName1.setText("");
                    txtUserName1.requestFocus();
                    return false;
                }
            }
        }
        String pwd = pwfPass.getText();
        if (pwd.equals("")) {
            JOptionPane.showMessageDialog(this, "You must input password!");
            pwfPass.requestFocus();
            return false;
        }
        if (pwd.length() < 6) {
            JOptionPane.showMessageDialog(this, "length Password > 6 character!");
            pwfPass.requestFocus();
            return false;
        }
        return true;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnRegister;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPasswordField pwfPass;
    private javax.swing.JRadioButton rdAdministrator;
    private javax.swing.JRadioButton rdStatusOFF;
    private javax.swing.JRadioButton rdStatusON;
    private javax.swing.JRadioButton rdUser;
    private javax.swing.JTextField txtUserName1;
    // End of variables declaration//GEN-END:variables
}
